package com.yf.thumbbackend.controller;

import com.yf.thumbbackend.common.BaseResponse;
import com.yf.thumbbackend.common.ResultUtils;
import com.yf.thumbbackend.constant.UserConstant;
import com.yf.thumbbackend.entity.User;
import com.yf.thumbbackend.exception.BusinessException;
import com.yf.thumbbackend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录
     * @param loginuserId
     * @param request
     * @return
     */
    @GetMapping("/login")
    public BaseResponse<User> login( long loginuserId, HttpServletRequest request) {
       if (loginuserId == 0) {
          throw  new BusinessException(400, "用户不存在");
       }

        User byId = userService.getById(loginuserId);
       if (byId == null) {
          throw  new BusinessException(400, "用户不存在");
       }
       request.getSession().setAttribute(UserConstant.LOGIN_USER, byId);
       return ResultUtils.success(byId);

    }
    @GetMapping("/get/login")

    public BaseResponse<User> getLoginUser (HttpServletRequest request){
       User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(loginUser);

    }
}
